﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GeneratorLibrary.Settings {
    public partial class ChangeSettings : Form {
        public ChangeSettings() {
            InitializeComponent();
            SettingsHelper.Initialize();
        }

        private void ChangeSettings_Load(object sender, EventArgs e) {
            BindSettings();
            
            string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string defaultFile = "myGeneratorSettings.xml";
            
            openFileDialog1.InitialDirectory = desktop;
            saveFileDialog1.InitialDirectory = desktop;

            openFileDialog1.FileName = defaultFile;
            saveFileDialog1.FileName = defaultFile;
        }

        private void BindSettings() {
            txtDatabaseServer.Text = Addin.Default.DatabaseServer;
            txtDatabaseServerList.Text = Addin.Default.DatabaseServerList;
            txtGrantRoles.Text = Addin.Default.GrantRoles;
            txtIsActiveField.Text = Addin.Default.IsActiveField;
            txtOrderByFields.Text = Addin.Default.OrderByFields;

            txtClassGenerators.Text = Addin.Default.ClassGenerators;
            txtDefaultClassGenerator.Text = Addin.Default.DefaultClassGenerator;
            txtAddReferences.Text = Addin.Default.AddReferenceList;
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e) {
            Addin.Default.DatabaseServer = txtDatabaseServer.Text;
            Addin.Default.DatabaseServerList = txtDatabaseServerList.Text;
            Addin.Default.GrantRoles = txtGrantRoles.Text;
            Addin.Default.IsActiveField = txtIsActiveField.Text;
            Addin.Default.OrderByFields = txtOrderByFields.Text;

            Addin.Default.ClassGenerators = fixClassGenList(txtClassGenerators.Text);
            Addin.Default.AddReferenceList = fixAddRefList(txtAddReferences.Text);

            //after class generator list is set, then we can set the default class generator.
            Addin.Default.DefaultClassGenerator = fixDefaultClassGen(txtDefaultClassGenerator.Text);
            
            Addin.Default.Save();
            this.Close();
        }

        private string fixAddRefList(string input) {
            string[] list = input.Split(new char[] { ';' });
            for(int i = 0;i<list.Length;i++) {
                string[] keyval = list[i].Split(new char[] { '=' }, 2);
                for (int j = 0; j < keyval.Length; j++) {
                    keyval[j] = keyval[j].Trim();
                }
                list[i] = String.Join("=", keyval);
            }
            return String.Join(";\n", list);
        }

        private string fixDefaultClassGen(string input) {
            input = input.Trim();
            string[] list = Addin.Default.ClassGenerators.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < list.Length; i++) {
                string[] keyval = list[i].Split(new char[] { '=' }, 2);
                for (int j = 0; j < keyval.Length; j++) {
                    keyval[j] = keyval[j].Trim();
                }
                if (keyval[1] == input)
                    return input;
                if (keyval[0] == input)
                    return keyval[1];
            }
            return "";//unknown value
        }

        private string fixClassGenList(string input) {
            string[] list = input.Split(new char[] { ',' });
            for (int i = 0; i < list.Length; i++) {
                string[] keyval = list[i].Split(new char[] { '=' }, 2);
                for (int j = 0; j < keyval.Length; j++) {
                    keyval[j] = keyval[j].Trim();
                }
                list[i] = String.Join("=", keyval);
            }
            return String.Join(",\n", list);
        }

        private void btnRestoreDefaults_Click(object sender, EventArgs e) {
            string title = "Are you sure you want to restore the default settings?";
            if (MessageBox.Show(title, title, MessageBoxButtons.YesNo) == DialogResult.Yes) {
                SettingsHelper.SetToDefaults();
                this.Close();
            }
        }

        private void btnImport_Click(object sender, EventArgs e) {
            if (openFileDialog1.ShowDialog() == DialogResult.OK) {
                try {
                    SettingsHelper.SetToFile(new FileInfo(openFileDialog1.FileName));
                } catch (Exception ex){
                    MessageBox.Show("File could not be imported... Error: " + ex.Message);
                }
                BindSettings();
                MessageBox.Show("Done!");
            }
        }

        private void btnExport_Click(object sender, EventArgs e) {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
                SettingsHelper.ExportToFile(saveFileDialog1.FileName);
                MessageBox.Show("Done!");
            }
        }

    }
}
